/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amuthu;

import jargs.gnu.CmdLineParser;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.amuthu.parser.ExpressionEvaluator;

/**
 *
 * @author Prabhus
 */
public class TechInd
{
   public static void main(String[] args) throws SQLException
   {
      if (args.length == 0)
      {
         printUsage();
         System.exit(2);
      }

      CmdLineParser parser = new CmdLineParser();

      CmdLineParser.Option file = parser.addStringOption('f', "file");
      CmdLineParser.Option date = parser.addStringOption('d', "date");
      CmdLineParser.Option days = parser.addIntegerOption("ndays");
      CmdLineParser.Option volume = parser.addIntegerOption("avgvol");
      CmdLineParser.Option aprice = parser.addDoubleOption("avgprice");
      CmdLineParser.Option adays = parser.addIntegerOption("avgdays");

      try
      {
         parser.parse(args);
      }
      catch (CmdLineParser.OptionException e)
      {
         System.err.println(e.getMessage());
         printUsage();
         System.exit(2);
      }

      TradingDate today = new TradingDate();
      String fileName = (String) parser.getOptionValue(file);
      String lastDate = (String) parser.getOptionValue(date, today.toString("yyyy-mm-dd"));
      Integer nbDays = (Integer) parser.getOptionValue(days, new Integer(10));
      Integer avgVolume = (Integer) parser.getOptionValue(volume, new Integer(0));
      Double avgPrice = (Double) parser.getOptionValue(aprice, new Double(0.00));
      Integer avgDays = (Integer) parser.getOptionValue(adays, new Integer(0));

      if (fileName == null)
      {
         System.out.println("Enter the file name that contains the expression");

         System.exit(2);
      }

      File filetotest = new File(fileName);
      String expression = null;

      if (!filetotest.exists())
      {
         System.out.println("File specified does not exist");

         System.exit(2);
      }
      try
      {
          expression = readFileAsString(filetotest.getAbsolutePath());
      }
      catch (IOException ex)
      {
         Logger.getLogger(TechInd.class.getName()).log(Level.SEVERE, null, ex);
         System.exit(2);
      }

      try
      {
         today = new TradingDate(lastDate, TradingDate.BRITISH);
      }
      catch (TradingDateFormatException ex)
      {
         Logger.getLogger(TechInd.class.getName()).log(Level.SEVERE, null, ex);
      }

      EodDatabase database = new EodDatabase("nse_eod_data", "localhost", "3306", "root", "jas124mine");

      ArrayList<String> symbols = new ArrayList<String>(1000);
      database.getSymbols(symbols);
      QuoteDataStore datastore = QuoteDataStore.getInstance();
      ExpressionEvaluator expr = new ExpressionEvaluator(expression);
      //ExpressionEvaluator expr = new ExpressionEvaluator("sar(0.02,0.2) > close");
      //ExpressionEvaluator expr = new ExpressionEvaluator("close > open");
      GlobalFilter globalFilter = new GlobalFilter(avgVolume, avgPrice, avgDays);
      boolean success;

//      int noofdays = expr.computeDayDependency();
//      System.out.println(noofdays);

      for (int i = 0; i < symbols.size(); i++)
      {
         datastore.clean();
         //database.loadQuote(symbols.get(i), today, avgPrice, avgVolume, avgDays, nbDays);
         success = database.loadQuote(symbols.get(i), today, globalFilter, nbDays);

         if (success)
         {
            double res = expr.computeExpression();

            if (res >= 1.0)
            {
               System.out.println(symbols.get(i));
            }
         }
      }
   }

   private static String readFileAsString(String filePath) throws java.io.IOException
   {
      byte[] buffer = new byte[(int) new File(filePath).length()];
      BufferedInputStream f = null;
      try
      {
         f = new BufferedInputStream(new FileInputStream(filePath));
         f.read(buffer);
      }
      finally
      {
         if (f != null)
         {
            try
            {
               f.close();
            }
            catch (IOException ignored)
            {
            }
         }
      }
      return new String(buffer);
   }

   private static void printUsage()
   {
      System.err.println(
              "Usage: tech_scanner [{-f, --file} file_name ] \n[{-d, --date} last date eg - 2011-04-19]\n [{--ndays} number of days to scan]");
   }

}
